$(function () {
    showCondition(true, 0, 0, 1, []);
    var monitorType = 'depGroup';
    var scoreQuota = [{
        "quotaCode": "TotalScore",
        "quotaName": "综合得分"
    }, {
        "quotaCode": "CapacityScore",
        "quotaName": "产能得分"
    }, {
        "quotaCode": "EfficiencyScore",
        "quotaName": "效率得分"
    }, {
        "quotaCode": "QualityScore",
        "quotaName": "安全得分"
    }];
    paramTabPage[tabId].drg = perfQuotaDrg;
    paramTabPage[tabId].reg = perfQuotaReg;
    /*获取科室综合*/
    getDepGroupScore(scoreQuota, "depGroup", "loadDepGroupNextLayerPerf");
    /* 获取全部参与科室组绩效指标比较汇总的科室组*/
    drawHomePerfRards(perfQuotaDrg, perfQuotaReg);
    /* 全院各科室组DRGs指标综合比较汇总表*/
    loadTotalQuotaTable(totalQuota, "depGroup", "loadDepGroupNextLayerPerf", perfQuotaDrg, perfQuotaReg);

    /**
     *  @ Discription:获取科室组名称和编码
     * @Autor ：liuhongjun
     * @param sx
     * @param totalQuota
     * @param type
     * @returns绘制复选框 any ywcHospPerf.js 2017年6月7日上午11:25:23
     */
    function drawHomePerfRards(perfQuotaDrg, perfQuotaReg) {
        //绘制指标复选框
        loadQuotaCheckbox();

        // 获取科室组数据
        $.ajax({
            url: contextPath + '/DrgsIndexCal/getDepGroupByCode.dhc',
            data: {
                'dateFrom': paramTabPage[tabId].dateFrom,
                'dateTo': paramTabPage[tabId].dateTo,
                'hospCode': paramTabPage[tabId].hospCode
            }, // 参数值
            dataType: "json",
            type: "post",
            success: function (result) {
                if (result.success) {
                    var depGroupAll = result.data;
                    if (jQuery.isEmptyObject(depGroupAll)) {// 没有科室组
                        $('#' + tabId + ' #depGroupCodes ul').html("无数据");
                    } else {
                        var str = "";
                        // 绘制复选框
                        $('#' + tabId + ' #depGroupCodes ul').html("");
                        $.each(depGroupAll, function (n, value) {
                            if (n == 0) {
                                str += "<label><input checked='checked' type='checkbox' name='depGroupcode' value='"
                                    + value.CODE
                                    + "' descval='"
                                    + value.NAME
                                    + "'/>"
                                    + value.NAME + "&nbsp;&nbsp;</label>";
                            } else {
                                str += "<label><input type='checkbox' name='depGroupcode' value='"
                                    + value.CODE
                                    + "' descval='"
                                    + value.NAME
                                    + "'/>"
                                    + value.NAME + "&nbsp;&nbsp;</label>";
                            }
                        });
                        str += "<label><input type='checkbox' id='hspstd' name='depGroupcode' value='hspstd' descval='全院均值' />全院均值&nbsp;&nbsp;</label>";
                        $('#' + tabId + ' #depGroupCodes ul').append(str);
                        if (depGroupAll.length > 5) {
                            var strShowMore = "<div id='showMore'><a href='#' style='text-decoration:none'><i class='icon-plus-sign'style='font-size: 25px;'></i></a></div>";
                            // 添加已经选中的复选框
                            $('#' + tabId + ' #depGroupCodes').append(strShowMore);
                            var $category = $('#' + tabId + ' #depGroupCodes ul label:gt(6)');
                            $category.hide();
                            var $toggleBtn = $('#' + tabId + ' #showMore a');
                            $toggleBtn.toggle(function () {
                                $category.show();
                                $(this).find('i').removeClass('icon-plus-sign').addClass('icon-minus-sign').css('color', 'red');
                            }, function () {
                                $category.hide();
                                $(this).find('i')
                                    .removeClass('icon-minus-sign')
                                    .addClass('icon-plus-sign')
                                    .css('color', '#428BCA');
                            });
                        }
                        // 复选框绑定事件，绘制指标雷达图
                        $('#' + tabId + ' input[name="depGroupcode"]:checkbox').on('ifChanged', function () {
                            if ($('#' + tabId + ' input[name="depGroupcode"]:checkbox:checked').size() === 0) {
                                $(this).attr('checked', 'checked');
                            }
                            drawRadarBySelectedQuota(monitorType);
                        });
                    }
                }
                //初始化icheck
                $('#' + tabId + ' input').iCheck({
                    checkboxClass: 'icheckbox_square-blue',
                    radioClass: 'iradio_square-blue',
                    increaseArea: '10%' // optional
                });
                $('#' + tabId + ' #quotaPanel input').on('ifChanged', function () {
                    drawRadarBySelectedQuota(monitorType);
                });
                drawRadarBySelectedQuota(monitorType);
            }
        });
    }

    /*查看全院医师*/
    $("#" + tabId + " #lookAllDoctor_id").click(function () {
        var param = jQuery.extend(true, {}, paramTabPage[tabId]);
        param.upOnLevel = 'Hops';
        param.monitorType = 'Hops';
        param.isA = 0;
        openTab('/pages/yizheng/perf/ywcHospDoctorPerf.jsp', "全部|" + '医生比较', 'Hosp_Doctor_1', 'Hosp_Doctor_1', param);
    });
});

function medAffHspPerfJointStr(table) {
    var quotaCodes = ["DRGCount", "CMI", "AvgDays", "TimeEff", "AvgFee", "FeeEff", "WeightPerDoctor", "WeightPerBedDays", "Risk1Ratio", "Risk2Ratio", "InRatio"];
    var quotaNames = ["DRGs组数", "CMI", "平均住院日", "时间效率指数", "次均费用(元)", "费用效率指数", "每医师权重", "每床日权重", "低风险死亡率(%)", "中低风险死亡率(%)", "入组率(%)"];
    var str = "";
    for (var i = 0; i < quotaCodes.length; i++) {
        str += "<tr><td>" + quotaNames[i] + "</td>";
        var hsp = table[0][quotaCodes[i]] == null ? -1 : table[0][quotaCodes[i]];
        var reg = table[1][quotaCodes[i]] == null ? -1 : table[1][quotaCodes[i]];
        var hspstd = table[2][quotaCodes[i]] == null ? -1 : table[2][quotaCodes[i]];
        if (hsp == -1 || reg == -1 || hspstd == -1) {
            if (hsp == -1) {
                str += "<td>-</td>";
            } else {
                str += "<td>" + hsp + "</td>";
            }
            if (reg == -1) {
                str += "<td>-</td>";
            } else {
                str += "<td>" + reg + "</td>";
            }
            if (hspstd == -1) {
                str += "<td>-</td>";
            } else {
                str += "<td>" + hspstd + "</td>";
            }
            str += "</tr>"
        } else {
            switch (quotaCodes[i]) {
                //正向
                case "DRGCount":
                case "CMI":
                case "WeightPerDoctor":
                case "WeightPerBedDays":
                case "InRatio":
                    if (hsp < reg && hsp < hspstd) {
                        str += "<td><font color='red'>" + hsp + "</font></td>";
                    } else {
                        str += "<td>" + hsp + "</td>";
                    }
                    str += "<td>" + reg + "</td>";
                    str += "<td>" + hspstd + "</td>"
                    str += "</tr>"
                    break;
                //负向
                case "AvgDays":
                case "TimeEff":
                case "AvgFee":
                case "FeeEff":
                case "Risk1Ratio":
                case "Risk2Ratio":
                    if (hsp > reg && hsp > hspstd) {
                        str += "<td><font color='red'>" + hsp + "</font></td>";
                    } else {
                        str += "<td>" + hsp + "</td>";
                    }
                    str += "<td>" + reg + "</td>";
                    str += "<td>" + hspstd + "</td>"
                    str += "</tr>"
                    break;
            }
        }
    }
    return str;
}

/**
 *
 * @ Discription：分层配置跳转
 * @Autor ：liuhongjun
 * @param code
 * @param desc
 * @returns
 * any
 * ywcHospPerf.js
 * 2017年6月7日下午8:41:49
 */
function loadDepGroupNextLayerPerf(code, desc) {
    var param = jQuery.extend(true, {}, paramTabPage[tabId]);
    param.depGroupCode = code;
    var destUrl = "";//目标页面的url
    var destPageTitle = "";//目标页面的title
    var destPageId = "";//目标页面的ID
    if (commonLayerConfig.length >= 4) {
        if (commonLayerConfig[1] == "1") {
            destPageTitle = desc + "|" + "科室绩效";
            destPageId = code + "depGroup_Dep_Perf";
            destUrl = '/pages/yizheng/perf/ywcDepPerf.jsp';
        } else if (commonLayerConfig[2] == "1") {
            destPageTitle = desc + "|" + "病区绩效";
            destPageId = code + "depGroup_Ward_Perf";
            destUrl = '/pages/yizheng/perf/ywcWardPerf.jsp';
        }
    }
    openTab(destUrl, destPageTitle, destPageId, destPageId, param);
}

/**
 * 科室组得分分析
 * @param {} totalQuota
 * @param {} monitorType
 * @param {} functionName
 */
function getDepGroupScore(scoreQuota, monitorType, functionName) {
    var columns = [{
        "data": "ddesc",
        "title": "科室组",
        "render": function (obj, type, full) {
            return "<a title ='" + full.ccode
                + "' href=\"javascript:"
                + functionName + "('"
                + full.ccode + "','"
                + full.ddesc + "') \">"
                + full.ddesc + "</a>";
        }
    }, {
        "data": "QuotaValue",
        "title": "综合得分"
    }, {
        "data": "CapacityScore",
        "title": "产能得分"
    }, {
        "data": "EfficiencyScore",
        "title": "效率得分"
    }, {
        "data": "QualityScore",
        "title": "安全得分"
    }, {
        "data": "",
        "title": "操作",
        "render": function (obj, type, full) {
            var isA = 0
            if (toNextLayerBoolPerf(monitorType)) {
                isA = 1;
            } else {
                isA = 0;
            }
            return "<a class='jumplink' href=\"javascript:loadHospDoctor('" + full.ccode + "','" + full.ddesc + "','" + monitorType + "','" + isA + "')\">医生比较</a>";
        }
    }];
    var datas = [];
    var ajaxCnt = scoreQuota.length;
    for (var i in scoreQuota) {
        var quotaCode = scoreQuota[i].quotaCode;
        getPerfScoreTable(quotaCode, monitorType,
            function (quotaCoden, data) {
                var data_s = [];
                data_s = data_s.concat(data);
                if (datas.length <= 0) {
                    datas = data_s;
                }
                mergeListcc(datas, data_s, 'ccode', '', quotaCoden);
                ajaxCnt--;
            });
    }
    var timer = setInterval(function () {
        if (ajaxCnt == 0) {
            clearInterval(timer);
            for (var i in datas) {
                datas[i].QuotaValue = datas[i].TotalScore;
            }
            var paramsDataTables = {order:[[1,'desc']],ordering:true};
            $("#" + tabId + " #depGroupScore_table").drgsDataTable(columns, datas, paramsDataTables);
        }
    }, 200);
};
